package com.chinda.common.utils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.chinda.common.enums.MessageEnum;
import com.chinda.common.exception.RRException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.util.CharsetUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.InputStream;

/**
 * Response工具类
 *
 * @author Wang Chinda
 * @date 2021/6/9
 * @see
 * @since 1.0
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ResponseUtil {

    /**
     * 输出Json
     *
     * @param res
     * @param r
     */
    public static void outJson(HttpServletResponse res, R r) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            res.setContentType(MediaType.APPLICATION_JSON_VALUE);
            res.setCharacterEncoding(CharsetUtil.UTF_8.name());
            mapper.writeValue(res.getWriter(), r);
        } catch (Exception e) {
            log.error(MessageEnum.SYS_CHECK_0007.getMsg(), e);
            throw new RRException(MessageEnum.SYS_CHECK_0007.getMsg(), e);
        }

    }

    /**
     * 输出字节流
     * @param res
     * @param content   文件字节流
     * @param fileName  文件名
     * @param suffix    文件后缀
     */
    public static void outBytes(HttpServletResponse res, byte[] content, @NotNull String fileName, @NotNull String suffix) {
        try {
            res.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            res.setCharacterEncoding(CharsetUtil.UTF_8.name());
            res.setHeader(HttpHeaders.CONTENT_DISPOSITION, StrUtil.format("attachment; filename={}.{}", fileName, suffix) );
            res.addHeader(HttpHeaders.CONTENT_LENGTH, "" + content.length);
            ServletOutputStream out = res.getOutputStream();
            IoUtil.write(out, true, content);
        } catch (Exception e) {
            log.error(MessageEnum.SYS_SECURITY_0008.getMsg(), e);
            throw new RRException(MessageEnum.SYS_SECURITY_0008.getMsg(), e);
        }
    }

    /**
     * 输出InputStream
     * @param res
     * @param in   文件流
     * @param fileName  文件名
     * @param suffix    文件后缀
     */
    public static void outInputStream(HttpServletResponse res, InputStream in, @NotNull String fileName, @NotNull String suffix) {
        ServletOutputStream out = null;
        try {
            Assert.notNull(in, "InputStream is null!");
            res.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            res.setCharacterEncoding(CharsetUtil.UTF_8.name());
            res.setHeader(HttpHeaders.CONTENT_DISPOSITION, StrUtil.format("attachment; filename={}.{}", fileName, suffix));
            res.addHeader(HttpHeaders.CONTENT_LENGTH, "" + in.available());
            out = res.getOutputStream();
            IoUtil.copy(in, out);
        } catch (Exception e) {
            log.error(MessageEnum.SYS_SECURITY_0009.getMsg(), e);
            throw new RRException(MessageEnum.SYS_SECURITY_0008.getMsg(), e);
        } finally {
            IoUtil.close(in);
            IoUtil.close(out);
        }
    }
}
